package com.zeei.ems.meet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zeei.ems.base.page.PageParamVO;
import com.zeei.ems.base.util.JsonUtil;
import com.zeei.ems.base.util.MQPublishUtil;
import com.zeei.ems.base.util.SessionHelper;
import com.zeei.ems.base.util.StringUtil;
import com.zeei.ems.meet.constant.MQConstant;
import com.zeei.ems.meet.dao.MeetingRecordDao;
import com.zeei.ems.meet.dao.MeetingRecordDetailDao;
import com.zeei.ems.meet.dao.MeetingRecordUserDao;
import com.zeei.ems.meet.model.MeetingRecordUserVO;
import com.zeei.ems.meet.model.MeetingRecordVO;
import com.zeei.ems.meet.model.dto.MeetingDto;
import com.zeei.ems.meet.model.dto.MemberDto;
import com.zeei.ems.meet.model.vo.MeetingParam;
import com.zeei.ems.meet.model.vo.MeetingRecordParam;
import com.zeei.ems.meet.model.vo.MeetingRecordUserParam;
import com.zeei.ems.meet.service.MeetingRecordService;
import com.zeei.ems.meet.util.BeanMapperUtils;
import com.zeei.ems.meet.util.MeetingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class MeetingRecordServiceImpl implements MeetingRecordService {

    @Autowired
    MeetingRecordDao meetingRecordDao;
    @Autowired
    MeetingRecordDetailDao meetingRecordDetailDao;
    @Autowired
    MeetingRecordUserDao meetingRecordUserDao;

    @Override
    public PageInfo<MeetingRecordVO> listMeetingRecord(PageParamVO page, MeetingRecordParam meetingRecord) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<MeetingRecordVO> meetingRecordList = meetingRecordDao.listMeetingRecord(meetingRecord);
        return new PageInfo<>(meetingRecordList);
    }

    @Override
    public List<MeetingRecordVO> listMeetingRecord(MeetingRecordParam meetingRecord) {
        return meetingRecordDao.listMeetingRecord(meetingRecord);
    }

    @Override
    public MeetingRecordVO getMeetingRecord(MeetingRecordParam meetingRecord) {
        return meetingRecordDao.getMeetingRecord(meetingRecord);
    }

    @Override
    @Transactional
    public MeetingRecordVO addMeetingRecord(MeetingRecordVO meetingRecord) {
        if (meetingRecord.getUserId() == null){
            meetingRecord.setUserId(SessionHelper.getContactUserId());
        }
        Integer id = SessionHelper.getUserId();
        MeetingParam meetingVO = BeanMapperUtils.map(meetingRecord, MeetingParam.class);
        if (StringUtil.isNotEmpty(meetingRecord.getPassword())) {
            meetingVO.setOpenPasswordFlag(true);
        }else{
            meetingVO.setOpenPasswordFlag(false);
        }
        MeetingDto meetingDto = MeetingUtil.createMeeting(meetingVO);
        if (meetingDto != null) {
            meetingRecord.setMeetingUUID(meetingDto.getMeetingUUID());
            meetingRecord.setMeetingCode(meetingDto.getMeetingCode());
            meetingRecord.setMeetingStatus(0);
            Integer integer = meetingRecordDao.addMeetingRecord(meetingRecord);
            if (integer > 0) {
                List<Integer> userIdList = StringUtil.splitStrToList(meetingRecord.getMeetingJoiners());
                if (!CollectionUtil.isEmpty(userIdList)) {
                    List<MeetingRecordUserVO> meetingRecordUserList = new ArrayList<>();
                    userIdList.forEach(userId -> {
                        MeetingRecordUserVO meetingRecordUser = new MeetingRecordUserVO();
                        meetingRecordUser.setMeetingUUID(meetingDto.getMeetingUUID());
                        meetingRecordUser.setUserId(userId);
                        meetingRecordUserList.add(meetingRecordUser);
                    });
                    meetingRecordUserDao.addMeetingRecordUserList(meetingRecordUserList);

                    //sendMeetingMq(meetingDto.getMeetingUUID(), userIdList);
                }
            }

            return integer > 0 ? meetingRecord : null;
        }

        return null;
    }

    @Override
    public MemberDto joinMeetingRecord(String meetingCode, String password) {
        String userId = SessionHelper.getUserId().toString();
        MemberDto memberDto = null;
        if (StringUtil.isNotEmpty(userId)) {
            memberDto = MeetingUtil.joinMeeting(userId, meetingCode, password);
            memberDto.setUserId(Integer.parseInt(userId));
        }

        return memberDto;
    }

    @Override
    public Boolean endMeetingRecord(String meetingUUID) {
        MeetingDto meetingListMembers = MeetingUtil.getMeetingListMembers(meetingUUID);
        if (meetingListMembers != null && CollectionUtil.isEmpty(meetingListMembers.getMemberList())){
            meetingRecordDetailDao.addMeetingRecordDetail(meetingListMembers.getMemberList());
            meetingRecordDao.endMeetingRecord(meetingUUID);

            MeetingUtil.deleteMeeting(meetingUUID);
        }

        return Boolean.TRUE;
    }

    /**
     * 推送到会议队列
     * @param meetingUUID 会议编码
     * @param userIdList 参会人员
     */
    private void sendMeetingMq(String meetingUUID, List<Integer> userIdList){
        MeetingRecordUserParam meetingRecordUserParam = new MeetingRecordUserParam();
        meetingRecordUserParam.setMeetingUUID(meetingUUID);
        meetingRecordUserParam.setUserIdList(userIdList);
        List<MeetingRecordUserVO> meetingRecordUserList = meetingRecordUserDao.listMeetingRecordUser(meetingRecordUserParam);
        if (!CollectionUtil.isEmpty(meetingRecordUserList)){
            meetingRecordUserList.forEach(meetingRecordUser -> {
                String strMeetingRecordUser = JsonUtil.object2json(meetingRecordUser);
                MQPublishUtil.sendQueueMsg(MQConstant.MEETING_RECORD, strMeetingRecordUser);
            });
        }
    }
}
